Defined
A For Loop allows code to be repeatedly executed. It is classified as an iteration statment. Often distinguished by an explicit loop counter or loop variable, this allows the code that is being repeatedly executed to know about the sequencing of each iteration. For Loop is a counter that runs from the start to the end, with a constant increment.

Breaking Down a For Loop

for (initialize variable ; your condition ; increment variable)


Key Notes:
1. Only plug in "initialize variable", "your condition", and/or "an increment variable" if desired.
2. Notice the semicolons(;)? Each one is treated as its own statement.


Examples:

**//example 1**
   for (int i=0; i<5; i++)
     //initialize; condition; increment
**//example 2**
   **for (int i=5; i>0 && sum<20; i--)**
     //initialize; compound condition; increment
**//example 3**
   **int i=0;
     for (  ; i>5; i++)**
 
    //nothing in "initialize variable"?
    //it initializes variables but the variable has already been initialized (int i=0;)

Now then, to figure out what is happening in a For Loop, I will use the first example, but with a print statement occurring after each pass:
**//example 1**
   for (int i=0; i<5; i++)
   {
       System.out.println(i);
   }

The loop created above prints 0-4 on the screen with each number on a new line.
Now to see how it works:
1.Figure out what the default variable starts as, in this case, "i" is set to 0.
2.Check to see if the initialized variable meets the condition. In this case, yes, i < 5(0<5)
3.Go inside the loop and do whatever is needed. In this case, print whatever "i" is.
4.Go back to the beginning of the loop, and add 1 to "i". The ++ is the same as doing "i= i+1".
5.Repeat the loop, continuing to check if "i" meets the condition, then printing the value of it on a new line.
6.When "i" finally is equal to 5, it stops. Why? Well, considering 5(value of i) cannot be less than 5, it cannot meet the condition and continues on, ignoring the code in the loop.

Magic Numbers
Refrain from the use of magic numbers in your code. These numbers are specific values placed into the condition portion of your for loop. Despite the fancy name, they are not to be utilized in your programming. When you use a magic number, you are limited to that number in your code. Whenever you realize that a different number should be used, you must go to every instance of that magic number in your code and change it. By utiliting a variable instead, each instance of what would have been your magic number can be changed in one location. That technique is also useful for when you reuse your code from an older program and need to change it up a little.

When that condition of your for loop requires that counter variable to be less than the length of the array, it is helpful to use the ".length" property of the array. That way each value of your array can be accessed through your for loop.

Break and Return in Loops
Break can be used to take you out of a certain section of code. However, it is important to remember that break does not necessary have to end the code. An example of this is a break in a nested for loop. If the break is in the nested loop, it would only bring you out of the inner for loop and not the outer one.
Return on the other hand immediately kicks out of the code. Returns can be anywhere, whether its within a loop or not.
Example:
int [] numbers = new int [1, 2, 3, 4, 5];
for(int i = 0; i < numbers.length; i ++)
{
    int x = numbers[i];
 
    if(x % 2 == 5)
    {
        numbers[i] = 0;
    }
    else
    break;
}
 
For Loops with Array Lists
The use of array lists in your for loops are not very complicated. However, there are a few alterations that you need to make to your code to have the loop work properly. First, you have to have an array list made and set to contain the values you desire. Second, the condition stage in creating the for loop must have a limit of numbers.size() for an array list named numbers. Any time that you would typically use the array accessor method like "numers[i]", you must instead use the accessor method for array lists like "numbers.get(i). Using these simple steps, modifying your previous code that used arrays to use array lists will be easy.